package cn.dansj.utils.transfer;

import cn.dansj.common.utils.transfer.ArrayUtils;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class PermissionUtils {
    public enum Permission {
        Read("页面访问", 1), //0b1 00001
        Write("新增内容", 1 << 1), //0b10 00010
        Update("修改内容", 1 << 2), //0b100 00100
        Delete("删除内容", 1 << 3), //0b1000 01000
        Execute("执行操作", 1 << 4), //0b10000 10000
        ;
        private final String name;
        private final int binary;

        Permission(String name, int binary) {
            this.name = name;
            this.binary = binary;
        }

        public String getName() {
            return name;
        }

        public int getBinary() {
            return binary;
        }

        public static List<String> getAllName() {
            return Arrays.stream(Permission.values()).map(e -> e.name).collect(Collectors.toList());
        }
    }

    public static List<Permission> getAllPermission() {
        return ArrayUtils.asList(Permission.values());
    }

    public static int mergePermission(List<Integer> binary) {
        int p = 0;

        for (int b : binary) {
            p = p | b;
        }

        return p;
    }

    public static int mergePermission(Integer... binary) {
        return mergePermission(ArrayUtils.asList(binary));
    }

    public static String toBinaryString(int binary) {
        return Integer.toBinaryString(binary);
    }
}
